package com.example.leo.mycharmingstudent.project;

import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.leo.mycharmingstudent.R;
import com.example.leo.mycharmingstudent.project.adapter.MainTabAdapter;
import com.example.leo.mycharmingstudent.project.customview.NoScrollViewPager;
import com.example.leo.mycharmingstudent.project.maintabfragment.KnowledgeFragment;
import com.example.leo.mycharmingstudent.project.maintabfragment.MainPageFragment;
import com.example.leo.mycharmingstudent.project.maintabfragment.MineFragment;
import com.example.leo.mycharmingstudent.project.maintabfragment.ProjectFragment;
import com.example.leo.mycharmingstudent.project.maintabfragment.subpagefragment.wxpublic.PublicFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity
        implements View.OnClickListener,MainPageFragment.ScrollCallback {

    private static final String TAG = "MainActivity";
    //fragment名字的key
    public final static String FRAGMENT_NAME = "fragment_name";

    //标题
    private TextView tvTitle;
    //首页tab
    private TextView tvMainPage;
    //知识体系tab
    private TextView tvKnowledge;
    //公众号tab
    private TextView tvPublic;
    //项目tab
    private TextView tvProject;
    //我的tab
    private TextView tvMine;
    private MainPageFragment mMainPageFragment;

    private List<TextView> mTabList = new ArrayList<>();

    //fragment列表
    private List<Fragment> mFragmentList = new ArrayList<>();
    //加载fragment的ViewPager
    private NoScrollViewPager vpFragment;
    private LinearLayout llTab;

    private int MAX_ALPHA = 255;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViews();
        initData();
        initEvent();

    }

    private void findViews() {
        tvTitle = findViewById(R.id.tv_title_activity_main);
        vpFragment = findViewById(R.id.vp_fragment_activity_main);
        llTab = findViewById(R.id.ll_tab_activity_main);
        tvMainPage = findViewById(R.id.tv_main_page_activity_main);
        tvKnowledge = findViewById(R.id.tv_knowledge_activity_main);
        tvPublic = findViewById(R.id.tv_public_activity_main);
        tvProject = findViewById(R.id.tv_project_activity_main);
        tvMine = findViewById(R.id.tv_mine_activity_main);
    }

    private void initData() {
        /*
        此处注意，需要按照顺序添加
         */
        mTabList.add(tvMainPage);
        mTabList.add(tvKnowledge);
        mTabList.add(tvPublic);
        mTabList.add(tvProject);
        mTabList.add(tvMine);

        tvMainPage.setSelected(true);
        /*FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        mMainPageFragment = new MainPageFragment();
        transaction.add(R.id.fl_container_activity_main,mMainPageFragment);
        transaction.commit();*/
        mMainPageFragment = new MainPageFragment();
        mFragmentList.add(mMainPageFragment);
        mFragmentList.add(KnowledgeFragment.newInstance());
        mFragmentList.add(PublicFragment.newInstance("公众号"));
        mFragmentList.add(ProjectFragment.newInstance("项目"));
        mFragmentList.add(MineFragment.newInstance("我的"));

        /*
        值传递：传递数值，一方发生改变，另一方吃喝玩乐不受影响。
        址传递：传递地址，一方发生改变，另一方也跟着发生改变。
        mFragmentList是址传递。什么现象？
        MainActivity中的mFragmentList发生改变，比如添加了一个Fragment。
        MainTabAdapter中接收的mFragmentList也会发生改变，添加了一个Fragment。
        因为：虽然是两个变量，但是指向的都是同一个对象。
        一方操作对象，另一方获得到的对象也会受影响。
         */
        vpFragment.setAdapter(
                new MainTabAdapter(getSupportFragmentManager(),
                        mFragmentList));
    }

    private void initEvent() {
        //标题点击事件
        tvTitle.setOnClickListener(this);

        tvMainPage.setOnClickListener(this);
        tvKnowledge.setOnClickListener(this);
        tvPublic.setOnClickListener(this);
        tvProject.setOnClickListener(this);
        tvMine.setOnClickListener(this);

        mMainPageFragment.setScrollCallback(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_title_activity_main:
                //滚动到顶部
                mMainPageFragment.scrollToTop();
                break;
            case R.id.tv_main_page_activity_main:
                //显示首页Fragment
                vpFragment.setCurrentItem(0);
                tvTitle.setText("首页");
                setSelectTab(0);
                break;
            case R.id.tv_knowledge_activity_main:
                //显示知识体系Fragment
                vpFragment.setCurrentItem(1);
                tvTitle.setText("知识体系");
                setSelectTab(1);
                break;
            case R.id.tv_public_activity_main:
                //显示公众号Fragment
                vpFragment.setCurrentItem(2);
                tvTitle.setText("公众号");
                setSelectTab(2);
                break;
            case R.id.tv_project_activity_main:
                //显示项目Fragment
                vpFragment.setCurrentItem(3);
                tvTitle.setText("项目");
                setSelectTab(3);
                break;
            case R.id.tv_mine_activity_main:
                //显示我的Fragment
                vpFragment.setCurrentItem(4);
                tvTitle.setText("我的");
                setSelectTab(4);
                break;
            default:
                Log.e(TAG, "onClick: I don't know this onclick,please check!");
                break;
        }
    }

    private void setSelectTab(int selectedPosition) {
        int size = mTabList.size();
        /*
        为什么使用i < size这种形式？
        1.i < mTabList.size()表示执行语句，每一次循环都会调用一次mTabList.size()方法
        2.进行列表删除条目操作的时候，会导致数组越界（了解）
         */
        for (int i = 0; i < size; i++) {
            if (i == selectedPosition) {
                mTabList.get(i).setSelected(true);
            } else {
                mTabList.get(i).setSelected(false);
            }
        }
    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        float diff = llTab.getAlpha() * 255 - dy;
        float alpha = diff/MAX_ALPHA;
        if(alpha < 0){
            llTab.setAlpha(0.0f);
            llTab.setVisibility(View.GONE);
        }else if(alpha >= 1){
            llTab.setAlpha(1.0f);
            llTab.setVisibility(View.VISIBLE);
        }else {
            llTab.setVisibility(View.VISIBLE);
            llTab.setAlpha(alpha);
        }
    }

    @Override
    public void onLeaveScreen() {
        float alpha = llTab.getAlpha();
        if(alpha > 0){
            llTab.setAlpha(1.0f);
            llTab.setVisibility(View.VISIBLE);
        }else {
            llTab.setAlpha(0.0f);
            llTab.setVisibility(View.GONE);
        }
    }
}
/*
 * 创建包的目的是为了分类
 * 只要符合分类，可以随意建包
 */
